var modelAddModule=angular.module('wfModuleAddApp', []);  
modelAddModule.controller('wfModuleAddCtr',['$scope', '$http',function($scope, $http) {

	$scope.getOrganizaAdd = function(){
			$('#organizaAdd').modal({remote: basePath+'/basicData/department/department_wfmodule_selList.jsp'})
			 $('#organizaAdd').on("hidden.bs.modal",function(){
					$(this).removeData("bs.modal");
				   });
	};
	$scope.getflowAdd = function(){
		   //$('#flowAdd').modal({remote: basePath+'/system/flowManager/wf_selList.jsp'});
			$('#organizaAdd').modal({remote: basePath+'/system/flowManager/wf_selList.jsp'})
			 $('#organizaAdd').on("hidden.bs.modal",function(){
					$(this).removeData("bs.modal");
				   });
	};
	$scope.clearSel = function(){ 
		$('#depName').val(''); 
		$('#depCode').val(''); 
	};
	$scope.clearlc = function(){ 
		$('#flowName').val('');
		$('#version').val('');
	    $('#flowId').val(''); 
	};
		// 点击保存按钮，新增一条数据
		$scope.saveWFModule = function() {
			// form表单的验证 数据不对，return false
			var formValid = $scope.wfModuleFormAdd.$valid;
			$scope.moduleCode=$('#moduleCode').val();
			$scope.depName=$('#depName').val();
			$scope.depCode=$('#depCode').val();
			$scope.flowName=$('#flowName').val();
			$scope.version=$('#version').val();
			$scope.flowId=$('#flowId').val();
			if (!$scope.moduleCode) {
				toastr.warning("业务模块不可为空！");
				formValid = false;
				return false;
			}
			
			if (!$scope.flowName) {
				toastr.warning("流程不可为空！");
				formValid = false;
				return false;
			}
			if (!$scope.version) {
				toastr.warning("流程版本不可为空！");
				formValid = false;
				return false;
			}
			
			if (formValid) { // 表单通过验证后提交
				check(); // 校验客户名称是否重复
				//submitData();//提交
			}
		};

		function check() {
			var result = true;
			$http({
				url : basePath+'/flowManager/checkFlowDepRelation',
				method : 'POST',
				params : {
					'flowId' : $scope.flowId,
					"moduleCode": $scope.moduleCode,
				}
			}).success(function(data, header, config, status) {
				if (data.rmsg == '0') { // 没有重复的属性名称
					submitData();
				} else {
					toastr.warning("业务模块流程重复！");
					return;
				}
			}).error(function(data, header, config, status) {
				// 处理响应失败
				result = false;
			});
		}
		
		// form表单提交方法 submit(若是校验都通过啦，数据便可以提交啦)
		function submitData() {
			$http({
				url : basePath+'/flowManager/insertFlowDepRelation',
				method : 'POST',
				params : {
					'flowId'  : $scope.flowId, // 流程Id
					'flowName'  : $scope.flowName, // 流程名称
					'version'  : $scope.version, // 流程版本
					'depId' : $scope.depCode, // 部门Id
					'depName' : $scope.depName, // 部门名称
					'moduleCode' : $scope.moduleCode, //业务模块
					
				}
			}).success(function(data, header, config, status) {
				if(data.rmsg===0){
					toastr.success("操作失败");
				}else{
				 toastr.success("操作成功");
				}
				setTimeout(function() {
					location.href = basePath+'/system/flowManager/wfmodule_list.jsp';
				}, 500);
			}).error(function(data, header, config, status) {
				toastr.error("调用错误！");
				setTimeout(function() {
					location.href = basePath+'/system/flowManager/wfmodule_list.jsp';
				}, 500);
			});
		}
		
		//  点击取消按钮，跳转到查询页面
		$scope.backHis = function() {
			location.href = basePath+'/system/flowManager/wfmodule_list.jsp';
		};
}]);